//
//  MBProgressHUD+Add.m
//  Walker
//
//  Created by 代佳宏 on 2017/7/5.
//  Copyright © 2017年 DaiJIaHong. All rights reserved.
//

#import "MBProgressHUD+Add.h"

#define MBBackgroundColor [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8]
#define MBTextColor       [UIColor whiteColor]

@implementation MBProgressHUD (Add)

#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view withMode:(MBProgressHUDMode)mode withProgress:(CGFloat)progress afterDelay:(CGFloat)afterDelay {
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }

    MBProgressHUD *hud = [MBProgressHUD HUDForView:view];

    if (hud == nil) {
        hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.removeFromSuperViewOnHide = YES;
        hud.backgroundColor = [UIColor clearColor];
        hud.backgroundView.backgroundColor = [UIColor clearColor];
        hud.backgroundView.layer.backgroundColor = [UIColor clearColor].CGColor;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = [UIColor clearColor];
        hud.bezelView.layer.backgroundColor = MBBackgroundColor.CGColor;
    }
   
    // 再设置模式
    hud.mode = mode;
    hud.label.textColor = MBTextColor;
    hud.label.numberOfLines = 0;
    hud.label.text = text;

    BOOL isProgress = NO;

    switch (mode) {
        case MBProgressHUDModeIndeterminate:{
            if (@available(iOS 9.0, *)) {
                UIActivityIndicatorView *appearance = [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]];
                appearance.color = MBTextColor;
            }
            else {
                [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
            }
        }
        case MBProgressHUDModeCustomView:{
            if (icon.length) {
                hud.customView = [[UIImageView alloc] initWithImage:IMG(icon)];
            }
            hud.customView.backgroundColor = [UIColor clearColor];
        }
            break;
        case MBProgressHUDModeDeterminate:
        case MBProgressHUDModeAnnularDeterminate:
        case MBProgressHUDModeDeterminateHorizontalBar:{
            hud.progress = progress;
            isProgress = YES;
        }
            break;
        default:{
            
        }
            break;
    }
   
    if (isProgress) {
        if (afterDelay <= 0) {
            afterDelay = 0.7;
        }
        if (progress <= 1) {
            [hud hideAnimated:YES afterDelay:afterDelay];
        }
    }
    else {
        if (afterDelay > 0) {
            CGFloat tempAfterDelay = text.length / 10.0;
            if (tempAfterDelay < afterDelay) {
                tempAfterDelay = afterDelay;
            }
            [hud hideAnimated:YES afterDelay:tempAfterDelay];
        }
    }
}


#pragma mark - 成功或失败信息,默认一秒钟消失
+ (void)showError:(NSString *)error toView:(UIView *)view {
    [MBProgressHUD show:error icon:nil view:view withMode:MBProgressHUDModeCustomView withProgress:0 afterDelay:0.7];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
    [MBProgressHUD show:success icon:nil view:view withMode:MBProgressHUDModeCustomView withProgress:0 afterDelay:0.7];
}

#pragma mark - 提示信息,默认一秒钟消失
+ (void)showTip:(NSString *)tip toView:(UIView *)view {
    [MBProgressHUD show:tip icon:nil view:view withMode:MBProgressHUDModeText withProgress:0 afterDelay:0.7];
}

+ (void)showTip:(NSString *)tip toView:(UIView *)view after:(CGFloat)afterDelay {
    [MBProgressHUD show:tip icon:nil view:view withMode:MBProgressHUDModeText withProgress:0 afterDelay:0.7];
}

+ (void)showTip:(NSString *)message withImage:(NSString *)image toView:(UIView *)view {
    [MBProgressHUD show:message icon:image view:view withMode:MBProgressHUDModeCustomView withProgress:0 afterDelay:0.7];
}

+ (void)showTip:(NSString *)message withImage:(NSString *)image toView:(UIView *)view after:(CGFloat)afterDelay {
    [MBProgressHUD show:message icon:image view:view withMode:MBProgressHUDModeCustomView withProgress:0 afterDelay:afterDelay];
}

#pragma mark - loading信息，不消失
+ (void)loadMessage:(NSString *)message toView:(UIView *)view {
    [MBProgressHUD show:message icon:nil view:view withMode:MBProgressHUDModeIndeterminate withProgress:0 afterDelay:0.0];
}

+ (void)loadMessage:(NSString *)message withImage:(NSString *)image toView:(UIView *)view {
    [MBProgressHUD show:message icon:image view:view withMode:MBProgressHUDModeIndeterminate withProgress:0 afterDelay:0.0];
}

+ (void)loadHideWithAnimate:(BOOL)animate toView:(UIView *)view {
    if (view == nil)
        view = [UIApplication sharedApplication].keyWindow;
    [MBProgressHUD hideHUDForView:view animated:animate];
}

#pragma mark - progress
+ (void)showProgress:(CGFloat)progress toView:(UIView *)view {
    [MBProgressHUD showProgress:progress withType:MBProgressHUDModeAnnularDeterminate toView:view];
}

+ (void)showProgress:(CGFloat)progress withMessage:(NSString *)message toView:(UIView *)view {
    [MBProgressHUD showProgress:progress withType:MBProgressHUDModeAnnularDeterminate withMessage:message toView:view];
}

+ (void)showProgress:(CGFloat)progress withType:(MBProgressHUDMode)mode toView:(UIView *)view {
    [MBProgressHUD showProgress:progress withType:mode withMessage:nil toView:view];
}

+ (void)showProgress:(CGFloat)progress withType:(MBProgressHUDMode)mode withMessage:(NSString *)message toView:(UIView *)view {
    [MBProgressHUD showProgress:progress withType:mode withMessage:message toView:view after:0];
}

+ (void)showProgress:(CGFloat)progress withType:(MBProgressHUDMode)mode withMessage:(NSString *)message toView:(UIView *)view after:(CGFloat)afterDelay {
    [MBProgressHUD show:message icon:nil view:view withMode:mode withProgress:progress afterDelay:afterDelay];
}

@end
